package com.platform.modules.prj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.RestResponse;
import com.platform.modules.prj.dto.PrjSuperviseListByAppDto;
import com.platform.modules.prj.entity.PrjSuperviseEntity;
import com.platform.modules.prj.service.PrjSuperviseService;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.quality.vo.QlyInspectionItemListByAppVo;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysOrgService;
import com.platform.modules.sys.service.SysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Controller
 *
 * @author Zhouxw
 * @date 2019-06-20 17:17:28
 */
@Api(tags = "PrjSuperviseController|项目监管")
@RestController
@RequestMapping("prj/supervise")
public class PrjSuperviseController extends AbstractController {
	
    @Autowired
    private PrjSuperviseService prjSuperviseService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysOrgService sysOrgService;


  
    @ApiOperation(value = "分配监管人员", notes = "分配监管人员")
    @PostMapping("/save")
    public RestResponse save(PrjSuperviseEntity prjSuperviseEntity) {
    	prjSuperviseService.saveOrUpdate(prjSuperviseEntity);
        return RestResponse.success();
    }
    
    @ApiOperation(value = "删除分配监管人员", notes = "删除分配监管人员")
    @DeleteMapping("/remove")
    public RestResponse remove(String id) {
    	prjSuperviseService.removeById(id);
        return RestResponse.success();
    }
    
    @ApiOperation(value = "删除分配监管人员", notes = "删除分配监管人员")
    @DeleteMapping("/remove/{id}")
    public RestResponse removeById(@PathVariable(value="id") String id) {
    	prjSuperviseService.removeById(id);
        return RestResponse.success();
    }
    

    @ApiOperation(value = "查询人员项目分配情况", notes = "查询人员项目分配情况")
    @GetMapping("/list")
    public RestResponse query(Integer currPage, Integer limit, String userId, String prjName) {
    	Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("prjName", prjName);
        params.put("page", PageUtil.getCurrPage(currPage));
        params.put("limit", PageUtil.getLimit(limit));
    	Page<PrjSuperviseEntity> page = prjSuperviseService.queryPage(params);
        return RestResponse.success().put("page", page);
    }

    @ApiOperation(value = "查询人员项目分配情况", notes = "查询人员项目分配情况")
    @PostMapping("/listByApp")
    public CommonResult<Page<PrjSuperviseEntity>> listByApp(@RequestBody  @Validated PrjSuperviseListByAppDto prjSuperviseListByAppDto) {
    	Map<String, Object> params = new HashMap<>();
    	
        String userId = prjSuperviseListByAppDto.getUserId();
        SysUserEntity sysUser = sysUserService.getById(userId);
        if(null==sysUser || StringUtils.isEmpty(sysUser.getOrgNo())) {
        	CommonResult.error("用户未找到");
        }
        SysOrgEntity sysOrg = sysOrgService.getById(sysUser.getOrgNo());
        
        //判断用户所属机构的类型是否是"项目机构"(项目机构的用户有orgNo, 但是orgNo是项目ID)
        if(null==sysOrg) {
        	params.put("prjId", sysUser.getOrgNo());
        }else {
        	params.put("userId", prjSuperviseListByAppDto.getUserId());
        }
        
        params.put("page", PageUtil.getCurrPage(prjSuperviseListByAppDto.getCurrPage()));
        params.put("limit", PageUtil.getLimit(prjSuperviseListByAppDto.getLimit()));
    	Page<PrjSuperviseEntity> page = prjSuperviseService.queryPage(params);
        return CommonResult.success(page);
        
    }
    
    
    
    
    
    
}
